package com.huike.web.controller;

import com.github.pagehelper.PageInfo;
import com.huike.entity.Department;
import com.huike.entity.Employee;
import com.huike.entity.Permission;
import com.huike.entity.Role;
import com.huike.query.EmployeeQuery;
import com.huike.query.QueryObject;
import com.huike.service.DepartmentService;
import com.huike.service.PermissionService;
import com.huike.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    @RequestMapping("/list")
    public ModelAndView getList(@ModelAttribute("qo") QueryObject qo, ModelAndView mav){
        PageInfo<Role> page = roleService.getPage(qo.getCurrentPage(), qo.getPageSize());
//        System.out.println(page.getList());
//        System.out.println(page.getPageSize());
//        mav.addObject("qo",qo);
        mav.addObject("pageInfo",page);
        mav.setViewName("role/list");
        return mav;
    }

    @RequestMapping("/input")
    public String input(Model model,Long id){
        Role role = roleService.getRoleById(id);
        List<Permission> permissions=permissionService.getAll();
        model.addAttribute("role",role);
        model.addAttribute("permissions",permissions);
        return "/role/input";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] permissionIds){
//        System.out.println(role);
//        System.out.println(permissionIds);
        roleService.saveOrUpdate(role,permissionIds);
        return "redirect:/role/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list";
    }
}
